import cgi
import os

from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
from droidguide_classes import *

    
class Process(webapp.RequestHandler):
  def get(self):
    operation = self.request.get("operation")
    entity = self.request.get("entity")
    key = self.request.get("key")

    startDroidGuideMessage(self)
    
    #http://localhost:8080/service/process?operation=delete&userToService_key=agpkcm9pZGd1aWRlchQLEg1Vc2VyVG9TZXJ2aWNlGIwBDA
    if operation == 'delete':
        deleteObject(key)
    
    elif operation == 'list':
        listObjects(entity, self)

    elif operation == 'listDevices':
        listObjects(Device, self)

    elif operation == 'get':
        obj = getObject(key)
        self.response.out.write(obj.to_xml())

    else:
        self.response.out.write('unsupported operation')
    
    finishDroidGuideMessage(self)

application = webapp.WSGIApplication([('/system/process', Process)], debug=True)

def main():
  run_wsgi_app(application)

if __name__ == '__main__':
  main()
